package com.cdz.service.impl;



import com.cdz.commen.R;
import com.cdz.domain.ChargingPileInfo;
import com.cdz.form.InitBleForm;
import com.cdz.form.MessageForm;
import com.cdz.form.NpnForm;
import com.cdz.form.StatusForm;
import com.cdz.mapper.ChargingPileInfoMapper;
import com.cdz.service.PileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

import static com.cdz.enums.ErrorEnum.*;

@Slf4j
@Service
public class PileServiceImpl implements PileService {

    @Resource
    private ChargingPileInfoMapper chargingPileInfoMapper;


    @Override
    public R<?> analysisMessage(MessageForm messageForm) {
        System.out.println("【接收message】的数据："+messageForm);
        String chargingPileId = messageForm.getChargingPileId();
        Integer BLE_BOUND = messageForm.getBLE_BOUND();
        int RELAY = messageForm.getRELAY();
        String ITemp = messageForm.getITemp();
        String UABC = messageForm.getUABC();
        String IABC = messageForm.getIABC();
        String PABC = messageForm.getPABC();
        String COTS = messageForm.getCOTS();
        int CP = messageForm.getCP();
        int CAUSE = messageForm.getCAUSE();
        int ERROR = messageForm.getERROR();
        String BLE_UID = messageForm.getBLE_UID();
        String BLE_MAC = messageForm.getBLE_MAC();
        int PNP_SW = messageForm.getPNP_SW();

        //插入到chargingPileInfo表
        ChargingPileInfo  chargingPileInfo = new ChargingPileInfo();
        chargingPileInfo.setChargingPileId(chargingPileId);
        chargingPileInfo.setBLE_BOUND(BLE_BOUND);
        chargingPileInfo.setRELAY(RELAY);
        chargingPileInfo.setITemp(ITemp);
        chargingPileInfo.setUABC(UABC);
        chargingPileInfo.setIABC(IABC);
        chargingPileInfo.setPABC(PABC);
        chargingPileInfo.setCOTS(COTS);
        chargingPileInfo.setCP(CP);
        chargingPileInfo.setCAUSE(CAUSE);
        chargingPileInfo.setERROR(ERROR);
        chargingPileInfo.setBLE_UID(BLE_UID);
        chargingPileInfo.setBLE_MAC(BLE_MAC);
        chargingPileInfo.setPNP_SW(PNP_SW);

        Timestamp updateTime =  new Timestamp(System.currentTimeMillis());
        System.out.println("【存储数据】存储到数据库的数据："+chargingPileInfo);
        chargingPileInfo.setUpdateTime(updateTime);
        chargingPileInfoMapper.updateChargingPileInfo(chargingPileInfo);
        return R.ok();
    }

    @Override
    public R<?> getStatus(String chargingPileId){
        System.out.println("准备查找充电桩"+chargingPileId+"的状态");
        ChargingPileInfo chargingPileInfo  =  chargingPileInfoMapper.getStatus(chargingPileId);
        StatusForm statusForm = new StatusForm();
        statusForm.setUABC(chargingPileInfo.getUABC());
        statusForm.setIABC(chargingPileInfo.getIABC());
        statusForm.setERROR(chargingPileInfo.getERROR());
        statusForm.setPABC(chargingPileInfo.getPABC());
        statusForm.setCP(chargingPileInfo.getCP());
        System.out.println("查找到的充电桩"+chargingPileId+"的状态："+statusForm);
        return R.ok(statusForm);

    }

    @Override
    public R<?> setPnpStatus(NpnForm npnForm) {
        String chargingPileId = npnForm.getChargingPileId();
        int PNP_SW = npnForm.getPNP_SW();
        if (chargingPileId == null || chargingPileId.isEmpty()) {
            return R.fail(ID_NO_EXXIST_ERROR);
        }
        if (PNP_SW != 0 && PNP_SW != 1) { // 假设0代表关闭，1代表开启
            return R.fail(DATA_NO_MATCH_ERROR);
        }
        chargingPileInfoMapper.setPnpStatus(chargingPileId, PNP_SW);
        System.out.println("设置充电桩"+chargingPileId+"的PNP_SW为："+PNP_SW);
        return R.ok();
    }

    @Override
    public R<?> getPiles() {
        List<ChargingPileInfo> list = chargingPileInfoMapper.getPiles();
        return R.ok(list);
    }

    @Override
    public R<?> getPNPStatus(String chargingPileId) {
        return R.ok(chargingPileInfoMapper.getPNPStatus(chargingPileId));
    }

    @Override
    public R<?> insertPileInfo(InitBleForm initBleForm) {
        if (chargingPileInfoMapper.ifExits(initBleForm.getChargingPileId())) {
            return R.ok("该充电桩ID已存在，无需初始化");
        }
        ChargingPileInfo chargingPileInfo = new ChargingPileInfo();
        chargingPileInfo.setChargingPileId(initBleForm.getChargingPileId());
        chargingPileInfo.setUABC("0");
        chargingPileInfo.setIABC("0");
        chargingPileInfo.setPABC("0");
        chargingPileInfo.setUpdateStatus(0);
        chargingPileInfo.setPNP_SW(0);
        chargingPileInfoMapper.initChargingPileInfo(chargingPileInfo);
        return R.ok("蓝牙设备初始化成功！");
    }

    @Override
    public R<?> emptyData(InitBleForm initBleForm) {
        String chargingPileId = initBleForm.getChargingPileId();
        ChargingPileInfo chargingPileInfo = new ChargingPileInfo();
        chargingPileInfo.setUABC(null);
        chargingPileInfo.setIABC(null);
        chargingPileInfo.setPABC(null);
        chargingPileInfo.setERROR(0); // 假设默认错误码为 0 表示正常
        chargingPileInfo.setCP(0);    // 默认状态为 0
        chargingPileInfoMapper.emptyDataByChargingPileId(chargingPileId);
        return R.ok();
    }

}

